<?php
/*
 * @Descripttion: 
 * @version: 
 * @Author: 奇辰科技
 * @Date: 2021-09-06 00:28:35
 * @LastEditors: lokei
 * @LastEditTime: 2022-10-28 19:38:22
 */

namespace App\Http\Controllers\Scrm\Fx;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Scrm\Fx\MemberModel;
use Illuminate\Http\Request;

class MemberController extends Controller
{
	public function ofMe(Request $request)
	{
		$user = app('auth')->user();
		$member = MemberModel::where('uid', '=', $user->uid)->first();
		$result = ResultTool::success();
		$result['data']['member'] = $member;
		return $result;
	}

	public function get(Request $request)
	{
		$level = MemberModel::where('id', '=', $request->input('id'))->first();
		$result = ResultTool::success();
		$result['data']['level'] = $level;
		return $result;
	}

	public function list()
	{
		$items = MemberModel::with(array('user'=>function($query) {
			$query->select('uid', 'avatar', 'nickname', 'mobile');
		}))->get();
		$result = ResultTool::success();
		$result['data']['items'] = $items;
		$result['data']['total'] = count($items);
		return $result;
	}

	public function register(Request $request)
	{
		$user = app('auth')->user();
		if(($user['avatar'] == '' || is_null($user['avatar'])) || ($user['nickname'] == '' || is_null($user['nickname'])) || ($user['mobile'] == '' || is_null($user['mobile']))) {
			$result = ResultTool::fail();
			$result['errorMsg'] = '请完善手机号、头像和昵称信息！';
			return $result;
		}
		$is_exist = MemberModel::where('uid', '=', $user->uid)->first();
		if(!is_null($is_exist)) {
			return ResultTool::fail();
		}
		$member = new MemberModel();
		$member->uid = $user->uid;
		$member->createtime = date('Y-m-d H:i:s', time());
		$member->status = "0";
		$member->money = 0;
		$member->withdrawed_money = 0;
		$member->follow_amount = 0;
		$member->save();
		$result = ResultTool::success();
		return $result;
	}

	public function pass(Request $request)
	{
		$member = MemberModel::where('id', '=', $request->input('id'))->first();
		$member->status = "1";
		$member->save();
		$result = ResultTool::success();
		return $result;
	}

	public function reject(Request $request)
	{
		$member = MemberModel::where('id', '=', $request->input('id'))->first();
		$member->status = "0";
		$member->save();
		$result = ResultTool::success();
		return $result;
	}

	public function create(Request $request)
	{
		$member = new MemberModel();
		$member->deduct_level = $request->input('deduct_level');
		$member->level_related = $request->input('level_related');
		$member->pre_level_id = $request->input('pre_level_id');
		$member->next_level_id = $request->input('next_level_id');
		$member->deduct_mode = $request->input('deduct_mode');
		$member->deduct_money = $request->input('deduct_money');
		$member->deduct_ratio = $request->input('deduct_ratio');
		$member->save();
		$result = ResultTool::success();
		return $result;
	}

	public function update(Request $request)
	{
		$member = MemberModel::where('id', '=', $request->input('id'))->first();
		$member->deduct_level = $request->input('deduct_level');
		$member->level_related = $request->input('level_related');
		$member->pre_level_id = $request->input('pre_level_id');
		$member->next_level_id = $request->input('next_level_id');
		$member->deduct_mode = $request->input('deduct_mode');
		$member->deduct_money = $request->input('deduct_money');
		$member->deduct_ratio = $request->input('deduct_ratio');
		$member->save();
		$result = ResultTool::success();
		return $result;
	}

	public function delete(Request $request)
	{
		MemberModel::where('id', '=', $request->input('id'))->delete();
		return ResultTool::success();
	}
}
